

#include "app_music.h"
#include "../../inc/pt_app.h"
//*******************************************************************************************

static music_para_t m_p;

//*******************************************************************************************

int32_t app_music_get_filenum(void)
{

	if(!m_p.p_fl_v){
		return 0;
	}

	if(!m_p.p_fl_v->f_total){
		return 0;
	}

	return m_p.p_fl_v->f_total;
}

int32_t app_music_switch(int32_t index)
{
	int ret  = -1;

	char music_name[1024] = {0};
	
	//************************************************************************************
	memset(music_name, 0, sizeof(music_name));
	//************************************************************************************

	m_p.p_err_id = 0;
	//************************************************************************************
	ret  = filelist_get_filename(m_p.p_fl_v,music_name,index);
	if((ret != 0)||(strlen(music_name) <= 0))
	{	
		m_p.p_err_id = 1;
		return -1;
	}
	//************************************************************************************
	log_i("i:t:n=%d:%d:%s\n",index,app_music_get_filenum(),music_name);
	//************************************************************************************
	de_music_file(music_name);
	//************************************************************************************


	//************************************************************************************

	return 0;
	
}


int32_t app_music_switch_prev(void)
{
	
	if(m_p.p_index <= 0){
		m_p.p_index = app_music_get_filenum() - 1; 
	}else{
		m_p.p_index -- ;
	}

	app_music_switch(m_p.p_index);

	return 0;
}


int32_t app_music_switch_next(void)
{
	m_p.p_index ++ ;
	if(m_p.p_index >= app_music_get_filenum()){
		m_p.p_index = 0; 
	}

	app_music_switch(m_p.p_index);

	return 0;
}




int32_t app_music_error_switch(void)
{
	if(!m_p.p_err_id)
		return -1;

	if(m_p.p_err_id == 1)
	{
		app_music_switch_next();
	}
	
	if(m_p.p_err_id == 2)
	{
		app_music_switch_prev();
	}

	m_p.p_err_id = 0;

	return 0;
}

void* app_music_msg_task(void *arg)
{
	int ret = -1;
	
	unsigned int cmd    = 0;
	unsigned int data0  = 0;
	unsigned int data1  = 0;
	
	while(1)
	{
		usleep(1000);
		//***********************************************
		ret = message_packet_recv(&cmd,&data0,&data1);
		if(ret != 0){
			continue;
		}

		switch(cmd){
		case MSG_VIDEO_END:
		{
			app_music_switch_next();

			break;
		}		
		case MSG_KEY_LEFT:
		{
			app_music_switch_prev();

			break;
		}	
		case MSG_KEY_RIGHT:
		{
			app_music_switch_next();

			break;
		}
		default:break;
		}
		//***********************************************

	}

}

void* app_music_autoplay_task(void *arg)
{
	while(1)
	{
		usleep(1000);

		//***********************************************
		
		app_music_error_switch();
		//***********************************************

	}

}
//*******************************************************************************************

int32_t app_music_create(void)
{
	
	int  ret  = -1;

	char search_path[1024];
	//********************************************************************************
	srand(time(NULL));
	//********************************************************************************
	log_i(" music app create\n");
	//********************************************************************************
	sprintf(search_path,"%s",CFG_MUSIC_PATH);
	
	log_i("search_path:%s\n",search_path);

	m_p.p_err_id  = 0;
	//********************************************************************************

	m_p.p_fl_v = filelist_cjson_create(search_path,FILE_TYPE_MUSIC,"music_file.tmp");
	if(!m_p.p_fl_v){
		log_e("create filelist error\n");
		return -1;
	}
	//********************************************************************************
	m_p.p_index = rand()%app_music_get_filenum();
	
	app_music_switch(m_p.p_index);
	//********************************************************************************

	pthread_create(&m_p.p_v_thread,   NULL, app_music_msg_task, NULL);
	pthread_create(&m_p.p_msg_thread, NULL, app_music_autoplay_task, NULL);
	//********************************************************************************

	return 0;
}

int32_t app_music_destory(void)
{
	if(!m_p.p_fl_v){
		return -1;
	}

	if(m_p.p_fl_v){
		filelist_cjson_destory(m_p.p_fl_v);
		m_p.p_fl_v = NULL;
	}

	log_i("music exit end\n");
		

	return 0;
}
